package com.risetechnology.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.risetechnology.customviews.CustomFontTextView;
import com.risetechnology.data.Method;
import com.risetechnology.krystalkitchen1.R;

public class MethodListAdapter extends ArrayAdapter<Method> {

	private Context context;
	private int layoutId;
	private List<Method> data = null;

	public MethodListAdapter(Context context, int layoutId, List<Method> data) {
		super(context, layoutId, data);
		this.context = context;
		this.layoutId = layoutId;
		this.data = data;
	}

	public View getView(final int position, View convertView,
			ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(layoutId, parent, false);

			holder = new ViewHolder();

			holder.textViewCotent = (CustomFontTextView) convertView.findViewById(R.id.tv_method);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		if (data.get(position).isSelected()) {
			convertView.setBackgroundColor(context.getResources().getColor(R.color.bg_calendar_item_inactive));
		} else {
			convertView.setBackgroundColor(Color.TRANSPARENT);
		}

		holder.textViewCotent.setText(data.get(position).getContent());

		final View tempView = convertView;
		convertView.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (data.get(position).isSelected()) {
					data.get(position).setSelected(false);
					tempView.setBackgroundColor(Color.TRANSPARENT);
				} else {
					data.get(position).setSelected(true);
					tempView.setBackgroundColor(context.getResources().getColor(R.color.bg_calendar_item_inactive));
				}
			}
		});

		return convertView;
	}

	static class ViewHolder {
		CustomFontTextView textViewCotent;
	}

}